package com.ele.ui;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

import com.ele.pojo.Shop;
import com.ele.service.AdminService;
import com.ele.service.ShopService;

/**
 * 	商铺管理输入界面
 * @author candy
 *
 */
public class AdminManager {
	private static Scanner sc = new Scanner(System.in);
	
	public void addShop() {
		Shop shop = new Shop();
		
		System.out.println("请输入商铺名字：");
		String shopName = sc.nextLine();
		shop.setShopName(shopName);
		System.out.println("请输入商铺地址：");
		String shopAddr = sc.nextLine();
		shop.setAddr(shopAddr);
		System.out.println("请输入商铺描述：");
		String shopDesc = sc.nextLine();
		shop.setShopDesc(shopDesc);
		System.out.println("请输入商铺起送费：");
		Double startPrice = sc.nextDouble();
		shop.setStartPrice(startPrice);
		System.out.println("请输入商铺配送费：");
		Double fee = sc.nextDouble();
		shop.setFee(fee);
		
		shop.setRegistTime(new Date());
		System.out.println("请输入商铺状态：");
		Integer shopStatus = sc.nextInt();
		sc.nextLine();
		shop.setShopStatus(shopStatus);
		System.out.println("请输入商铺密码：");
		String pwd = sc.nextLine();
		shop.setPassword(pwd);
		
		AdminService adminService = new AdminService();
		Boolean flag = adminService.addShop(shop);
		if (flag) {
			System.out.println("添加成功");
		}else {
			System.out.println("添加失败");
		}
	}
	/**
	 * 	展示查询商铺信息界面
	 */
	public void selectShop() {
		AdminService adminService = new AdminService();
		List<Shop> list = adminService.selectShop();
		if (list.size() == 0) {
			System.out.println("现在没有添加任何商家呢");
		}else {
			for ( Iterator<Shop> it = list.iterator();it.hasNext(); ) {
				System.out.println("----------------------");
				System.out.println(it.next());
				System.out.println("----------------------");
			}
		}
	}

	/**
	 * 	修改店铺信息界面
	 */
	public void updateShop() {
		AdminService adminService = new AdminService();
		System.out.println("请输入要更新的商铺编号：");
		Integer id = sc.nextInt();
		sc.nextLine();
		Shop shop = adminService.selectShop(id);
		if (shop == null) {
			System.out.println("该编号不存在");
		}else {
			System.out.println(shop);
			updateMessage(shop);
			Boolean flag = adminService.updateShop(shop);
			if (flag) {
				System.out.println("修改成功！");
			}else {
				System.out.println("修改失败，请检查输入数据格式是否正确");
			}
		}

	}
	/**
	 * 	确认修改商铺信息
	 * @param shop	shop对象
	 */
	public static void updateMessage(Shop shop) {
		System.out.println("请选择想要更新的数据：");
		System.out.println("1.商铺名	2.地址	3.描述	4.起步价	 5.配送费	  6.商铺状态	7.商铺密码");
		String input = sc.nextLine();
		if (input.equals("1")) {
			System.out.println("请输入更改之后的名字：");
			shop.setShopName(sc.nextLine());
		}else if (input.equals("2")) {
			System.out.println("请输入更改之后的地址：");
			shop.setAddr(sc.nextLine());
		}else if (input.equals("3")) {
			System.out.println("请输入更改之后的描述：");
			shop.setShopDesc(sc.nextLine());
		}else if (input.equals("4")) {
			System.out.println("请输入更改之后的起步价：");
			shop.setStartPrice(Double.parseDouble(sc.nextLine()));
		}else if (input.equals("5")) {
			System.out.println("请输入更改之后的配送费：");
			shop.setFee(Double.parseDouble(sc.nextLine()));
		}else if (input.equals("6")) {
			System.out.println("请输入更改之后的状态：");
			shop.setShopStatus(Integer.parseInt(sc.nextLine()));
		}else if (input.equals("7")) {
			System.out.println("请输入更改之后的密码：");
			shop.setPassword(sc.nextLine());
		}else {
			System.out.println("输入不正确！");
		}
		System.out.println("是否继续修改？y/n");
		if (sc.nextLine().equalsIgnoreCase("y")) {
			updateMessage(shop);
		}else {
			ShopService ss = new ShopService();
			int x = ss.updateShop(shop);
			if (x == 1) {
				System.out.println("修改成功");
			}
		}
	}
}
